package com.bskj.sj.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.utils.EncryptUtil;
import com.bskj.common.utils.LogUtils;
import com.bskj.common.utils.ResultMapUtil;
import com.bskj.common.web.BaseController;
import com.bskj.sj.service.IBmsUserService;

/**
 * Created by mocaijin on 23/01/2018.
 */
@Controller
@RequestMapping("/authInfo")
public class BmsUserController extends BaseController {

    @Autowired
    private IBmsUserService bmsUserService;


    /**
     * 商家的绑定商家账号页面
     *
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("page/bindWx")
    public ModelAndView pageBindWx(String openId, String appId) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/auth/register");
        mv.addObject("openId", openId);
        mv.addObject("appId", appId);

        JSONObject params = new JSONObject();
        params.put("uri", "/authInfo/bindWx");
        params.put("openId", openId);
        params.put("appId", appId);
        mv.addObject("bindWxParams", EncryptUtil.encrypt(params.toJSONString()));

        JSONObject enterpriseParams = new JSONObject();
        enterpriseParams.put("uri", "/authInfo/center");
        enterpriseParams.put("openId", openId);
        enterpriseParams.put("appId", appId);
        mv.addObject("enterpriseParams", EncryptUtil.encrypt(enterpriseParams.toJSONString()));

        return mv;
    }

    /**
     * 没有权限的菜单，则打开页面
     *
     * @param custId
     * @return
     * @throws Exception
     */
    @RequestMapping("page/nopermission")
    public ModelAndView nopermission(Integer custId) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/auth/nopermission");
        mv.addObject("custId", custId);
        return mv;
    }

    /**
     * 绑定商家账号
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("bindWx")
    public Map<String, Object> bindWx(BmsUser authInfo) {
        Map<String, Object> resultMap = null;
        try {
            LogUtils.debug(JSONObject.toJSONString(authInfo));
            //  `type` int(11) DEFAULT NULL COMMENT '账户类别,1:商场，2:商场中商户',
            resultMap = bmsUserService.bindWx(authInfo);
            if (resultMap.get("error") == null) {
                return ResultMapUtil.genDefaultSuccessDataMap(resultMap.get("authInfo"));
            } else {
                return ResultMapUtil.genErrorMap(resultMap.get("error").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapUtil.genErrorMap("绑定失败");
        }
    }

    /**
     * 解除绑定商家账号
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("unBindWx")
    public Map<String, Object> unBindWx(Integer authId) {
        if (authId == null) {
            return ResultMapUtil.genErrorMap("解除绑定失败，请重试");
        }
        try {
        	BmsUser authInfo = new BmsUser();
            authInfo.setId(authId);
            boolean flag = bmsUserService.unBindWx(authInfo);
            if (flag) {
                return ResultMapUtil.genDefaultSuccessMap();
            } else {
                return ResultMapUtil.genErrorMap("解除绑定失败，请重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapUtil.genErrorMap("删除失败");
        }
    }


    /**
     * 账号中心
     *
     * @param
     * @return
     */
    @RequestMapping("center")
    public ModelAndView center(Integer custId, Integer shopId, String openId, String appId) throws Exception {
        ModelAndView mv = new ModelAndView();
        LogUtils.debug(JSONObject.toJSONString(getRequest().getParameterMap()));
        String authId = getRequest().getParameter("authId");
        if (authId != null) {
        	 BmsUser bmsUser = bmsUserService.getByAuthId(Integer.parseInt(authId));
             LogUtils.debug("center:" + JSONObject.toJSONString(bmsUser));
             if (bmsUser != null) {
            	 mv.addObject("showName", bmsUser.getName());
                 mv.setViewName("/shop/center");
                 mv.addObject("custId", custId);

                 JSONObject params = new JSONObject();
                 params.put("uri", "/authInfo/unBindWx");
                 params.put("authId", authId);
                 params.put("custId", custId);
                 mv.addObject("unBindWxParams", EncryptUtil.encrypt(params.toJSONString()));

                 JSONObject bindWxPageParams = new JSONObject();
                 bindWxPageParams.put("uri", "/authInfo/page/bindWx");
                 bindWxPageParams.put("openId", bmsUser.getOpenId());
                 bindWxPageParams.put("appId", bmsUser.getAppId());
                 mv.addObject("bindWxPageParams", EncryptUtil.encrypt(bindWxPageParams.toJSONString()));

                 return mv;
			}
		}
        mv.setViewName("/auth/register");
        mv.addObject("openId", openId);
        mv.addObject("appId", appId);

        JSONObject params = new JSONObject();
        params.put("uri", "/authInfo/bindWx");
        params.put("openId", openId);
        params.put("appId", appId);
        mv.addObject("bindWxParams", EncryptUtil.encrypt(params.toJSONString()));

        JSONObject enterpriseParams = new JSONObject();
        enterpriseParams.put("uri", "/authInfo/center");
        enterpriseParams.put("openId", openId);
        enterpriseParams.put("appId", appId);
        mv.addObject("enterpriseParams", EncryptUtil.encrypt(enterpriseParams.toJSONString()));

        return mv;
    }
    
    @RequestMapping("/page/block")
    public ModelAndView block(Integer custId) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/auth/block");
        mv.addObject("custId", custId);
        return mv;
    }

}
